<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\YjPdf;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Illuminate\Support\Facades\Request;

class YjPdfController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */

    protected  $title  ;

    public function __construct()
    {
        $type = Request::get("type");
         switch ($type){
             case 1 :
                 $this->title = "历年真题上传";
                 break;
                 case 2 :
                 $this->title = "独家秘籍上传";
                 break;
             case 3 :
                 $this->title = "20年考试答案" ;
                 break ;
         }
    }
    protected function grid()
    {

        return Grid::make(new YjPdf(), function (Grid $grid) {
            $type = Request::get('type');
            $grid->model()->where(['type' => $type]);
            $grid->id->sortable();
            $grid->weight->sortable()->editable(true);
            $grid->pdf;
            $grid->notice;
            $grid->created_at->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('notice');

            });

            $grid->disableCreateButton();
            $url = request()->url();
            $grid->tools("<a href='{$url}/create?type={$type}' class='btn btn-primary disable-outline btn-outline'><i class='feather icon-plus'></i><span class='d-none d-sm-inline'>&nbsp;&nbsp;新增</span></a>");

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new YjPdf(), function (Show $show) {
            $show->id;
            $show->pdf;
            $show->notice;
            $show->created_at;
            $show->updated_at;
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new YjPdf(), function (Form $form) {
            $type = Request::get('type');
            $form->hidden('type','类型')->default($type);
            $form->display('id');
            $form->file('pdf')->required();
            $form->text("weight")->default(0) ;
            $form->text('notice');
            $form->display('created_at');
            $form->display('updated_at');

            $form->tools(function (Form\Tools $tools) use($type) {
                // 去掉跳转列表按钮
                $tools->disableList();

                $tools->append("<a href='/admin/pdf?type={$type}' class='btn btn-sm btn-white'><i class='feather icon-list'></i><span class='d-none d-sm-inline'>&nbsp;列表</span></a>");
            });
        });
    }
}
